package script;

import api.CommonApi;
import api.LoginApi;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import utils.HttpClientUtils;
import utils.ResponseData;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
import java.util.UUID;

public class TestVerifyCode {
    private static final Logger logger = LoggerFactory.getLogger(TestVerifyCode.class);

    public CommonApi commonApi = new CommonApi();
    @DataProvider
    public Object[][] buildData() throws IOException {
        String jsonStr = FileUtils.readFileToString(new File("./data/TestVerifyData.json"),"UTF-8");

        JSONArray jsonObject = JSONObject.parseArray(jsonStr);
        Object[][] testData = new Object[jsonObject.size()][];
        for (int i = 0;i<jsonObject.size();i++){
            JSONObject jsonObject1 = jsonObject.getJSONObject(i);
            Object type = jsonObject1.get("type");
            Object statusCode = jsonObject1.get("statusCode");
            testData[i] = new Object[]{type, statusCode};
        }
        logger.info("testData={}", Arrays.deepToString(testData));
        return testData;
    }
    @Test(dataProvider = "buildData")
    public void test01(String type,int statusCode) throws Exception {
        String random = "";
        if (type.equals("float")){
            random = new Random().nextDouble()+"";
        }else if ("int".equals(type)){
            random = new Random().nextInt()+"";

        }else if ("char".equals(type)){
            random = UUID.randomUUID().toString();

        }else if ("empty".equals(type)){
            random = "";
        }

        logger.info(random);
        ResponseData responseData = commonApi.getVerifyCode(random);
        Assert.assertEquals(responseData.getStatusCode(),statusCode);

    }


}
